var Material = Material || { version : '1.0' };

//Material公共配置集合，键值对象
Material.Config = Material.Config || {};

Material.Action = (function () {
    var process = null;
    var getBase = function () {
        if (!process) {
            process = new Material.Process();
        }
        return process;
    };
    var run = function (options) {
        getBase().render(options);
    };
    return {
        run: run
    };
})();

Material.Process = function (options ) {
    this.dao = Material.Dao;
};

Material.Dao = (function() {
    var getWorkflow = function (data, callback) {
        var data = {

    "workflowId": 62417,
    "transType": "ugc",
    "transId": 100000056,
    "status": "Finished",
    "stepInfoList": [
        {
            "id": 63518,
            "stepId": 1100,
            "dynamicInstanceId": -1,
            "subWorkflowId": 62418,
            "stepName": "成片反显",
            "taskId": 291519,
            "status": "Finished",
            "usedTime": "00:00:01",
            "nextIds": "1400",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63418,
            "stepId": 1000,
            "dynamicInstanceId": -1,
            "subWorkflowId": 62418,
            "stepName": "成片转码",
            "taskId": 291518,
            "status": "Finished",
            "usedTime": "00:00:21",
            "nextIds": "63518",
            "stepTipKey": "viewLog",
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63520,
            "stepId": 1100,
            "dynamicInstanceId": 168282,
            "subWorkflowId": 62419,
            "stepName": "PC端-流畅",
            "taskId": null,
            "status": "Finished",
            "usedTime": null,
            "nextIds": "63620",
            "stepTipKey": null,
            "index": 1,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63522,
            "stepId": 1100,
            "dynamicInstanceId": 168284,
            "subWorkflowId": 62419,
            "stepName": "PC端-超清",
            "taskId": null,
            "status": "Finished",
            "usedTime": null,
            "nextIds": "63622",
            "stepTipKey": null,
            "index": 3,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63521,
            "stepId": 1100,
            "dynamicInstanceId": 168283,
            "subWorkflowId": 62419,
            "stepName": "PC端-高清",
            "taskId": null,
            "status": "Finished",
            "usedTime": null,
            "nextIds": "63621",
            "stepTipKey": null,
            "index": 2,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63419,
            "stepId": 1000,
            "dynamicInstanceId": -1,
            "subWorkflowId": 62419,
            "stepName": "准备",
            "taskId": 291525,
            "status": "Finished",
            "usedTime": "00:00:00",
            "nextIds": "63520,63521,63522",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63720,
            "stepId": 1300,
            "dynamicInstanceId": 168282,
            "subWorkflowId": 62419,
            "stepName": "分发",
            "taskId": 291529,
            "status": "Finished",
            "usedTime": "00:00:05",
            "nextIds": "1700",
            "stepTipKey": null,
            "index": 1,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63721,
            "stepId": 1300,
            "dynamicInstanceId": 168283,
            "subWorkflowId": 62419,
            "stepName": "分发",
            "taskId": 291530,
            "status": "Finished",
            "usedTime": "00:00:05",
            "nextIds": "1700",
            "stepTipKey": null,
            "index": 2,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63722,
            "stepId": 1300,
            "dynamicInstanceId": 168284,
            "subWorkflowId": 62419,
            "stepName": "分发",
            "taskId": 291531,
            "status": "Finished",
            "usedTime": "00:00:05",
            "nextIds": "1700",
            "stepTipKey": null,
            "index": 3,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63620,
            "stepId": 1200,
            "dynamicInstanceId": 168282,
            "subWorkflowId": 62419,
            "stepName": "转码",
            "taskId": 291527,
            "status": "Finished",
            "usedTime": "00:00:47",
            "nextIds": "63720",
            "stepTipKey": "viewLog",
            "index": 1,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63621,
            "stepId": 1200,
            "dynamicInstanceId": 168283,
            "subWorkflowId": 62419,
            "stepName": "转码",
            "taskId": 291526,
            "status": "Finished",
            "usedTime": "00:00:49",
            "nextIds": "63721",
            "stepTipKey": "viewLog",
            "index": 2,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 63622,
            "stepId": 1200,
            "dynamicInstanceId": 168284,
            "subWorkflowId": 62419,
            "stepName": "转码",
            "taskId": 291528,
            "status": "Finished",
            "usedTime": "00:00:48",
            "nextIds": "63722",
            "stepTipKey": "viewLog",
            "index": 3,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1000,
            "stepId": 1000,
            "dynamicInstanceId": -1,
            "subWorkflowId": -1,
            "stepName": "介质反显",
            "taskId": 291515,
            "status": "Finished",
            "usedTime": "00:00:02",
            "nextIds": "1200",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1400,
            "stepId": 1400,
            "dynamicInstanceId": -1,
            "subWorkflowId": -1,
            "stepName": "准备",
            "taskId": null,
            "status": "Finished",
            "usedTime": null,
            "nextIds": "1500,1511,1522",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1200,
            "stepId": 1200,
            "dynamicInstanceId": -1,
            "subWorkflowId": -1,
            "stepName": "创建节目",
            "taskId": 291516,
            "status": "Finished",
            "usedTime": "00:00:01",
            "nextIds": "63418",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 111,
            "stepId": 111,
            "dynamicInstanceId": -1,
            "subWorkflowId": -1,
            "stepName": "开始",
            "taskId": null,
            "status": "Finished",
            "usedTime": null,
            "nextIds": "1000",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1500,
            "stepId": 1500,
            "dynamicInstanceId": 168279,
            "subWorkflowId": -1,
            "stepName": "开始转码",
            "taskId": 291520,
            "status": "Finished",
            "usedTime": "00:00:00",
            "nextIds": "63419",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 999,
            "stepId": 999,
            "dynamicInstanceId": -1,
            "subWorkflowId": -1,
            "stepName": "结束",
            "taskId": null,
            "status": "Queued",
            "usedTime": null,
            "nextIds": null,
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1611,
            "stepId": 1611,
            "dynamicInstanceId": 168280,
            "subWorkflowId": -1,
            "stepName": "缩略图分发",
            "taskId": 291523,
            "status": "Finished",
            "usedTime": "00:00:06",
            "nextIds": "999",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1511,
            "stepId": 1511,
            "dynamicInstanceId": 168280,
            "subWorkflowId": -1,
            "stepName": "缩略图转码",
            "taskId": 291521,
            "status": "Finished",
            "usedTime": "00:00:01",
            "nextIds": "1611",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1700,
            "stepId": 1700,
            "dynamicInstanceId": 168279,
            "subWorkflowId": -1,
            "stepName": "视频流生产完成",
            "taskId": null,
            "status": "Finished",
            "usedTime": null,
            "nextIds": "999",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        },
        {
            "id": 1522,
            "stepId": 1522,
            "dynamicInstanceId": 168281,
            "subWorkflowId": -1,
            "stepName": "预览转码",
            "taskId": 291522,
            "status": "Finished",
            "usedTime": "00:00:44",
            "nextIds": "999",
            "stepTipKey": null,
            "index": 0,
            "splitType": null,
            "splitName": null,
            "condition": null
        }
    ]

};
        /*
        var data = {
            "status": "Underway",
            "workflowId": 10498,
            "transId": 546,
            "transType": "program",
            "stepInfoList": [
                {
                    "id": 9005,
                    "index": 0,
                    "stepId": 0,
                    "splitType": 2,
                    "nextIds": "1400,1501,1502",
                    "condition": "start",
                    "stepName": "有无字幕"
                },
                {
                    "id": 111,
                    "index": 0,
                    "status": "Finished",
                    "stepId": 111,
                    "dynamicInstanceId": -1,
                    "taskId": 22303,
                    "splitType": 0,
                    "nextIds": "1000",
                    "subWorkflowId": -1,
                    "stepName": "开始"
                },
                {
                    "id": 1000,
                    "index": 0,
                    "status": "Finished",
                    "stepId": 1000,
                    "dynamicInstanceId": -1,
                    "taskId": 22304,
                    "splitType": 0,
                    "nextIds": "1100",
                    "subWorkflowId": -1,
                    "stepName": "成片转码",
                    "stepTipKey": "viewLog",
                    "usedTime": "00:00:18"
                },
                {
                    "id": 1100,
                    "index": 0,
                    "status": "Finished",
                    "stepId": 1100,
                    "dynamicInstanceId": -1,
                    "taskId": 22305,
                    "splitType": 2,
                    "nextIds": "9005",
                    "subWorkflowId": -1,
                    "stepName": "反显",
                    "usedTime": "00:00:00"
                },
                {
                    "id": 1400,
                    "index": 0,
                    "status": "Queued",
                    "stepId": 1400,
                    "dynamicInstanceId": -1,
                    //"condition": "no",
                    "splitType": 0,
                    "nextIds": "999",
                    "subWorkflowId": -1,
                    "stepName": "预览转码",
                    "stepTipKey": "viewLog"
                },
                {
                    "id": 999,
                    "index": 0,
                    "status": "Queued",
                    "stepId": 999,
                    "dynamicInstanceId": -1,
                    "splitType": 0,
                    "subWorkflowId": -1,
                    "stepName": "结束"
                },
                {
                    "id": 1501,
                    "index": 1,
                    "status": "Finished",
                    "stepId": 1500,
                    "dynamicInstanceId": 20775,
                    "condition": "yes",
                    "splitType": 0,
                    "nextIds": "1201",
                    "subWorkflowId": -1,
                    "stepName": "中文"
                },
                {
                    "id": 1201,
                    "index": 1,
                    "status": "Finished",
                    "stepId": 1200,
                    "dynamicInstanceId": 20775,
                    "taskId": 22307,
                    "splitType": 0,
                    "nextIds": "1301",
                    "subWorkflowId": -1,
                    "stepName": "字幕转码",
                    "stepTipKey": "viewLog",
                    "usedTime": "00:01:55"
                },
                {
                    "id": 1301,
                    "index": 1,
                    "status": "Underway",
                    "stepId": 1300,
                    "dynamicInstanceId": 20775,
                    "taskId": 22308,
                    "splitType": 0,
                    "nextIds": "1400",
                    "subWorkflowId": -1,
                    "stepName": "字幕分发"
                },
                {
                    "id": 1502,
                    "index": 2,
                    "status": "Finished",
                    "stepId": 1500,
                    "dynamicInstanceId": 20774,
                    "condition": "yes",
                    "splitType": 0,
                    "nextIds": "1202",
                    "subWorkflowId": -1,
                    "stepName": "中文"
                },
                {
                    "id": 1202,
                    "index": 2,
                    "status": "Finished",
                    "stepId": 1200,
                    "dynamicInstanceId": 20774,
                    "taskId": 22306,
                    "splitType": 0,
                    "nextIds": "1302",
                    "subWorkflowId": -1,
                    "stepName": "字幕转码",
                    "stepTipKey": "viewLog",
                    "usedTime": "00:02:02"
                },
                {
                    "id": 1302,
                    "index": 2,
                    "status": "Underway",
                    "stepId": 1300,
                    "dynamicInstanceId": 20774,
                    "taskId": 22309,
                    "splitType": 0,
                    "nextIds": "1400",
                    "subWorkflowId": -1,
                    "stepName": "字幕分发"
                }
            ]
        };
        */
        callback.call(this, data);
    };

    return {
        getWorkflow: getWorkflow
    };
})();

var render = function(options) {
    var params = {};
    var self = this;

    this.options = {
        rectWidth: 80,
        rectHeight: 40,
        spaceWidth: 30,
        spaceHeight: 20,
        fontSize: 12
    };
    this.params = params;
    this.$dialog = $('#MaterialProgressContainer').parent();
    this.$container = $('#' + options.id);
    this.isopen = false;

    // 依赖this.params参数去获取流程图的数据
    this._getDataAndDraw();
};

var _getDataAndDraw = function () {
    var self = this;
    self.$container.empty();
    this.dao.getWorkflow(self.params, function (data) {
        self.options['workflowStatus'] = data.status;
        self.options['workflowId'] = data.workflowId;
        self.options['data'] = self._createMatrix(data.stepInfoList);
        self.options['$tip'] = $("#SingleTip");

        // 每次绘制清空container
        self.$container.empty();
        self.$container.svgroutes(self.options);
    });
};

var _createMatrix = function (data) {
    var getAndPopItemById = function (id) {
        var result;
        data = data.filter(function (item) { return (item.id != id) || !(result = item); });
        return result;
    };
    // 让每一列的nextIds对应的item都能在下一列中找到，以此让矩阵变得‘饱满’。找不到的就创建假的item
    var fill = function () {
        matrix.forEach(function (line, lineIndex) {
            (lineIndex + 1 < matrix.length) && line.forEach(function (metaItem) {
                var missedId;
                metaItem.nextIds.forEach(function (id, index) {
                    if (matrix[lineIndex + 1].every(function (item) { return item.id != id; })) {
                        matrix[lineIndex + 1].push({
                            fake: true,
                            id: fakeId,
                            nextIds: ['' + id]
                        });
                        metaItem.nextIds[index] = '' + fakeId;
                        fakeId--;
                    }
                });
            });
        });
    };
    // 为每个item添加previousIds项，如同双向链表，方便后续处理
    var addPerviousIds = function () {
        matrix.forEach(function (line, lineIndex) {
            (lineIndex + 1 < matrix.length) && line.forEach(function (metaItem) {
                metaItem.nextIds.forEach(function (id) {
                    matrix[lineIndex + 1].some(function (nextLineItem) {
                        if (id == nextLineItem.id) {
                            nextLineItem.previousIds = (nextLineItem.previousIds || []).concat(['' + metaItem.id]);
                            return true;
                        }
                    });
                });
            });
        });
    };

    var startItem, matrix, fakeId = -1, debugCounter = 0;
    var self = this;

    data = data.map(function (item) {
        item.nextIds = item.nextIds ? item.nextIds.split(',') : [];
        item.rectWidth = self.options.rectWidth;
        item.rectHeight = self.options.rectHeight;
        return item;
    });

    startItem = getAndPopItemById(111);
    matrix = [[startItem]];
    while (!(matrix[matrix.length - 1].length == 1 && matrix[matrix.length - 1][0]['id'] == 999)) {
        matrix[matrix.length] = [];
        matrix[matrix.length - 2].forEach(function (metaItem) {
            metaItem.nextIds.forEach(function (id) {
                var i, rItem;
                var r = getAndPopItemById(id);
                if (!r && matrix.some(function (line, index) {
                    i = index;
                    return line.some(function (metaItem) { return !(rItem = metaItem) || (metaItem.id == id); });
                })) {
                    matrix[i] = matrix[i].filter(function (metaItem) { return metaItem.id != rItem.id; });
                    matrix[matrix.length - 1].push(rItem);
                } else {
                    matrix[matrix.length - 1].push(r);
                }
            });
        });
        if (debugCounter++ > 200) break; // TODO: 去除这里防止因为数据错误，找不到结束节点而进入死循环的代码
    }
    fill();
    addPerviousIds();
    return matrix;
};
